package com.longrise.co.ui.mine.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.appeaser.sublimepickerlibrary.datepicker.SelectedDate;
import com.appeaser.sublimepickerlibrary.recurrencepicker.SublimeRecurrencePicker;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.co.R;
import com.longrise.co.api.CommonPara;
import com.longrise.co.ui.mine.contract.MineContract;
import com.longrise.co.ui.mine.model.EditCarBean;
import com.longrise.co.ui.mine.model.MineModel;
import com.longrise.co.ui.mine.presenter.MinePresenter;
import com.longrise.co.utils.Constants;
import com.longrise.co.utils.DatePickerDialogUtils;
import com.longrise.co.utils.SublimePickerFragment;
import com.ly.common.base.BaseFragment;
import com.ly.common.commonutils.StringUtils;
import com.ly.common.commonutils.TimeUtil;
import com.ly.common.security.EnCryptDeCrypt2;

import java.util.Date;

import butterknife.Bind;
import butterknife.OnClick;

import static com.longrise.co.R.id.engine_number_et;
import static com.longrise.co.R.id.register_date_et;


/**
 * Created by luoyang on 2017-7-20.
 */

public class ModifyCarFragment extends BaseFragment<MinePresenter, MineModel> implements MineContract.View<EditCarBean>  {

    @Bind(R.id.head_title)
    TextView headTitle;
    @Bind(R.id.small_car_iv)
    ImageView smallCarIv;
    @Bind(R.id.middle_car_iv)
    ImageView middleCarIv;
    @Bind(R.id.big_car_iv)
    ImageView bigCarIv;
    @Bind(R.id.other_car_iv)
    ImageView otherCarIv;
    @Bind(R.id.car_no_et)
    TextView carNoEt;
    @Bind(R.id.frame_number_et)
    TextView frameNumberEt;
    @Bind(register_date_et)
    TextView registerDateEt;
    @Bind(engine_number_et)
    TextView engineNumberEt;

    private int mCurrentCarType = -1;

    private String mCarNo;
    private String mCarVin;
    private String mCarId;
    private String mEngineNumber;
    private int mCarType = -1;
    private String mRegisterDate = "请选择日期";

    private boolean isEditCarSuccess = false;

    @Override
    protected int getLayoutResource() {
        return R.layout.fg_modify_car;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
    }


    @Override
    protected void initView() {
        setText(headTitle, "编辑车辆");
        mCarNo = getArguments().getString("carNo");
        mCarVin = getArguments().getString("carVin");
        mCarId = getArguments().getString("carId");
        mEngineNumber = getArguments().getString("engineNumber");
        String carType = getArguments().getString("carType");
        String registerDate = getArguments().getString("registerDate");

        setText(carNoEt, mCarNo);
        setText(frameNumberEt, mCarVin);
        setText(engineNumberEt,mEngineNumber);

        if(!StringUtils.isEmpty(carType)) {
            mCarType = Integer.parseInt(carType);
            setCarType(mCarType);
        }

        if(!StringUtils.isEmpty(registerDate)) {
            mRegisterDate = TimeUtil.getDateYMD(registerDate);
            registerDateEt.setText(mRegisterDate);
        }
    }


    @OnClick({R.id.head_back, R.id.small_car_ll, R.id.middle_car_ll, R.id.big_car_ll, R.id.other_car_ll, R.id.confirm, register_date_et})
    public void onViewClicked(View view) {
        if (checkDoubleClick()) {
            switch (view.getId()) {
                case R.id.head_back:
                    _mActivity.onBackPressed();
                    break;

                case R.id.small_car_ll:
                    setCarType(1);
                    break;
                case R.id.middle_car_ll:
                    setCarType(2);
                    break;
                case R.id.big_car_ll:
                    setCarType(3);
                    break;
                case R.id.other_car_ll:
                    setCarType(9);
                    break;

                case R.id.confirm:
                    try {
                        if(mCurrentCarType == mCarType && mRegisterDate.equals(registerDateEt.getText().toString())) {
                            showLongToast("请选择车型或注册日期修改车辆");
                            return;
                        }
                        EntityBean bean = new EntityBean();
                        String enCarId = EnCryptDeCrypt2.getInstance().EnCrypt(mCarId);
                        bean.set("carid", mCarId);
                        bean.set("userflag", CommonPara.getInstance().getUserFlag());
                        if(mCurrentCarType != -1) {
                            bean.set("cartypecode", String.valueOf(mCurrentCarType));
                        }
                        if(!StringUtils.isEmpty(registerDateEt.getText()) && !"请选择日期".equals(registerDateEt.getText())) {
                            bean.set("registerdate", registerDateEt.getText().toString());
                        }
                        bean.set("user", CommonPara.getInstance().getUser());
                        bean.set("sign", enCarId);
                        mPresenter.editCar(bean);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    break;
                case register_date_et:
                    DatePickerDialogUtils.showDialog(mFragmentCallback, this.getChildFragmentManager());
                    break;

            }
        }

    }


    private void setCarType(int carType) {
        this.mCurrentCarType = carType;
        smallCarIv.setBackgroundResource(R.drawable.radio_unselect);
        middleCarIv.setBackgroundResource(R.drawable.radio_unselect);
        bigCarIv.setBackgroundResource(R.drawable.radio_unselect);
        otherCarIv.setBackgroundResource(R.drawable.radio_unselect);

        switch (mCurrentCarType) {
            case 1:
                smallCarIv.setBackgroundResource(R.drawable.radio_select);
                break;
            case 2:
                middleCarIv.setBackgroundResource(R.drawable.radio_select);
                break;
            case 3:
                bigCarIv.setBackgroundResource(R.drawable.radio_select);
                break;
            case 9:
                otherCarIv.setBackgroundResource(R.drawable.radio_select);
                break;
        }
    }

    @Override
    public void requestSuccess(EditCarBean bean) {
        showLongToast("修改车辆成功");
        isEditCarSuccess = true;
    }

    @Override
    public void onRequestComplete() {
        mRxManager.post(Constants.RX_EVENT_AUTH_CAR, "3");
        mRxManager.post(Constants.RX_EVENT_REFRESH_CAR_FILE, null);
        if(isEditCarSuccess) {
            _mActivity.onBackPressed();
        }
    }

    public static ModifyCarFragment newInstance(String carNo, String carVin, String carId, String carType, String registerDate, String engineNumber) {
        
        Bundle args = new Bundle();
        args.putString("carNo", carNo);
        args.putString("carVin", carVin);
        args.putString("carId", carId);
        args.putString("carType", carType);
        args.putString("registerDate",registerDate);
        args.putString("engineNumber",engineNumber);
        ModifyCarFragment fragment = new ModifyCarFragment();
        fragment.setArguments(args);
        return fragment;
    }


    SublimePickerFragment.Callback mFragmentCallback = new SublimePickerFragment.Callback() {
        @Override
        public void onCancelled() {

        }

        @Override
        public void onDateTimeRecurrenceSet(SelectedDate selectedDate,
                                            int hourOfDay, int minute,
                                            SublimeRecurrencePicker.RecurrenceOption recurrenceOption,
                                            String recurrenceRule) {
            Date date = selectedDate.getFirstDate().getTime();
            String newDateStr = TimeUtil.formatData(TimeUtil.dateFormatYMD, date.getTime() / 1000);
            registerDateEt.setText(newDateStr);
        }
    };

}
